//
//  main.m
//  vcf-oc
//
//  Created by 王成龙 on 16/5/9.
//  Copyright © 2016年 王成龙. All rights reserved.
//

#import <Foundation/Foundation.h>

//#import "PhoneData.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
             //1//
        
        
           // insert code here...
      /*   NSLog(@"Hello, World!");
         NSString *path=@"/Users/wangchenglong/Desktop/00001.vcf";
         NSString *arr;
         NSString *vcf=[[NSString alloc]initWithContentsOfFile:path  encoding:NSUTF8StringEncoding error:&arr];
            //NSLog(@"%@",vcf);
         NSArray *err = [vcf componentsSeparatedByString:@"\n"];
         PhoneData*item;
         NSMutableArray *tmp = [NSMutableArray new];
         for(NSString*p in err){
         if([p hasPrefix:@"BEGIN"])
         {
         item=[PhoneData new];
         
         
         }else if([p hasPrefix:@"END"])
         {
         [tmp addObject:item];
         }else if([p hasPrefix:@"TEL"])
         {
         
         NSArray *components=[p componentsSeparatedByString:@":"];
         NSString*app=components[1];
             app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
         item.tel=app;
             NSLog(@"电话号码： %@",app);
         }
         else if([p hasPrefix:@"FN:"]){
         NSArray *component=[p componentsSeparatedByString:@":"];
         NSString*abb=component[1];
         item.name=abb;
             NSLog(@"姓名：%@",abb);
         }
         
         }//NSLog(@"%@",tmp);
         return 0;
        
        
        */
        
                      //2//
        
        
        
         /*      NSString *path=@"/Users/wangchenglong/Desktop/PIM00001.vcf";
        NSString *arr;
        NSString *vcf=[[NSString alloc]initWithContentsOfFile:path  encoding:NSUTF8StringEncoding error:&arr];
        if(arr!=nil){
            NSLog(@"%@",arr);
            
            
        }
        //NSLog(@"%@",vcf);
        NSArray *err = [vcf componentsSeparatedByString:@"\n"];
        PhoneData*item;
        NSMutableArray *tmp = [NSMutableArray new];
        for(NSString*p in err){
            if([p hasPrefix:@"BEGIN"])
            {
                item=[PhoneData new];
                
                
            }else if([p hasPrefix:@"END"])
            {
                [tmp addObject:item];
                //1  NSLog(@"%@",item);
            }else if([p hasPrefix:@"TEL"])
            {
                
                NSArray *components=[p componentsSeparatedByString:@":"];
                NSString*app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);
                //1 item.tel=app;
            }
            else if([p hasPrefix:@"FN;"]){
                NSArray *component=[p componentsSeparatedByString:@":"];
                NSString*abb=component[1];
                abb = [abb stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                abb = [abb stringByRemovingPercentEncoding];
                NSLog(@"姓名：%@",abb);
                //1 item.name=abb;
            }
            
        }
        //NSLog(@"%@",tmp);
        return 0;
          */
        
        
                    //3//
        
        
        
/* NSString *path=@"/Users/wangchenglong/Desktop/00001.vcf";
        NSString *arr;
        NSString *vcf=[[NSString alloc]initWithContentsOfFile:path  encoding:NSUTF8StringEncoding error:&arr];
                //NSLog(@"%@",vcf);
        NSArray *err = [vcf componentsSeparatedByString:@"\n"];
        PhoneData*item;
      //  NSMutableArray *tmp = [NSMutableArray new];
        //new  相当于 alloc   init
        for(NSString*p in err){
            if([p hasPrefix:@"BEGIN"])
            {
                //item=[PhoneData new];
                NSLog(@"开始");
                
            }else if([p hasPrefix:@"END"])
            {
               // [tmp addObject:item];
                NSLog(@"结束");
            }else if([p hasPrefix:@"TEL"])
            {
                
                NSArray *components=[p componentsSeparatedByString:@":"];
                NSString*app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);

                //item.tel=app;
            }
            else if([p hasPrefix:@"FN:"]){
                NSArray *component=[p componentsSeparatedByString:@":"];
                NSString*abb=component[1];
                NSLog(@" 姓名：%@",abb);
            }
            
        }//NSLog(@"%@",tmp);
        return 0;
*/
                        //4//
        
        
       /* NSString *path=@"/Users/wangchenglong/Desktop/00001.vcf";
        NSString *arr;
        NSString *vcf=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&arr];
        NSArray *err = [vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
        for(NSString *line in err){
            if ([line hasPrefix:@"BEGIN"]) {
                NSLog(@"开始一条新的纪录");
                dict = [NSMutableDictionary new];
            }
            if ([line hasPrefix:@"END"]) {
                NSLog(@"这条纪录结束了。");
                [res addObject:dict];
            }
            if ([line hasPrefix:@"TEL"])
            {
                NSLog(@"找到一条电话纪录！%@",line);
                NSArray *components = [line componentsSeparatedByString:@":"];
//                NSString *last = tels.lastObject;
//                last = [last substringFromIndex:5];
                NSString*app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);
                dict[@"tel"] = app;
            }
            if ([line hasPrefix:@"FN"]) {
                NSArray *names = [line componentsSeparatedByString:@":"];
                NSString *name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"姓名:%@",name);
                dict[@"name"] = name;
            }
        }
        NSLog(@"%@",res);
        [res writeToFile:@"/Users/wangchenglong/Desktop/00001.vcf/vcf.txt"atomically:NO];
    }
    return 0;*/
        
        
                      // 5
       /* NSString * path=@"/Users/wangchenglong/Desktop/PIM00001.vcf";
        //NSString *arr;
        NSString * vcf=[[NSString alloc]initWithContentsOfFile:path encoding:4 error:nil];
        NSArray *err = [vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
        
        for(NSString *line in err)
        {
            if ([line hasPrefix:@"BEGIN"])
            {
                NSLog(@"开始一条新的纪录");
                dict = [NSMutableDictionary new];
            }
            if ([line hasPrefix:@"END"])
            {
                NSLog(@"这条纪录结束了。");
                [res addObject:dict];
            }
            if ([line hasPrefix:@"TEL"])
            {
                NSLog(@"找到一条电话纪录！%@",line);
                NSArray *components = [line componentsSeparatedByString:@":"];
               
                NSString*app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);
                dict[@"tel"] = app;
            }
            if ([line hasPrefix:@"FN"])
            {
                NSArray *names = [line componentsSeparatedByString:@":"];
                NSString *name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"姓名:%@",name);
                dict[@"name"] = name;
            }
        }
        //NSLog(@"%@",res);
        [res writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf.txt"atomically:NO];
        NSData * data= [NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        
        [data writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf2.txt" atomically:NO];
        //NSLog(@"%@",data);
        NSString *str = [[NSString alloc  ]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSMutableArray *res2 = [NSMutableArray new];
        for(NSDictionary *dict in res)
        {
            PhoneData *p = [PhoneData new];
            [p setValuesForKeysWithDictionary:dict];
            [res2 addObject:p];
        
        }
        NSLog(@"%@",res2);
    }
    return 0;
    */
 
        
        
                             //6//
        
       /* NSString *path = @"/Users/wangchenglong/Desktop/PIM00001.vcf";
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSArray  *err = [vcf componentsSeparatedByString:@"\n"];
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
        for(NSString *p in err){
            if([p hasPrefix:@"BEGIN"])
            {
                NSLog(@"开始");
                dict=[NSMutableDictionary new];
            }
            if([p hasPrefix:@"END"])
            {
                NSLog(@"结束");
                [res addObject:dict];
            }
            if([p hasPrefix:@"TEL"])
            {
                NSArray *components=[p componentsSeparatedByString:@":"];
                NSString *app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);
                dict[@"tel"] = app;

            }
            if([p hasPrefix:@"FN"])
            {
                NSArray *component=[p componentsSeparatedByString:@":"];
                NSString *abb=component[1];
                abb = [abb stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                abb = [abb stringByRemovingPercentEncoding];
                NSLog(@"姓名：%@",abb);
                dict[@"name"] = abb;
            
            }
      }
        [res writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf.txt"atomically:NO];
        NSData * data= [NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        
        [data writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf2.txt" atomically:NO];
        //NSLog(@"%@",data);
        NSString *str = [[NSString alloc  ]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSMutableArray *res2=[NSMutableArray new];
        for(NSDictionary *dict in res)
        {
            PhoneData *p = [PhoneData new];
            [p setValuesForKeysWithDictionary:dict];
            [res2 addObject:p];
            
            
        }
        NSLog(@"%@",res2);
        PhoneData *p = [PhoneData new];
        [p setDianhua:578];
        [p setDianhua:989];
        NSLog(@"%d",[p dianhua]);

        return 0;*/
        
        
        
        
        
                      //7//
     /*   NSString *path =@"/Users/wangchenglong/Desktop/PIM00001.vcf";
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:4 error:nil];
       // NSURL *url = [NSURL fileURLWithPath:@"/Users/wangchenglong/Desktop/PIM00001.vcf"];
       // NSString *vcf = [[NSString alloc]initWithContentsOfURL:url encoding:4 error:nil];
        NSArray *err = [vcf componentsSeparatedByString:@"\n"];
        //字典//
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
    
        for(NSString *p in err)
        {
            if([p hasPrefix:@"BEGIN"])
            {
                NSLog(@"开始");
                dict=[NSMutableDictionary new];
            }
            if([p hasPrefix:@"END"])
            {
                NSLog(@"结束");
                [res addObject:dict];
            }
            if([p hasPrefix:@"TEL"])
            {
                NSArray *components=[p componentsSeparatedByString:@":"];
                NSString *app=components[1];
                app = [app stringByReplacingOccurrencesOfString:@"-" withString:@""];
                app = [app stringByRemovingPercentEncoding];
                NSLog(@"电话号码：%@",app);
                dict[@"tel"] = app;
                
            }
            if([p hasPrefix:@"FN"])
            {
                NSArray *component=[p componentsSeparatedByString:@":"];
                NSString *abb=component[1];
                abb = [abb stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                abb = [abb stringByRemovingPercentEncoding];
                NSLog(@"姓名：%@",abb);
                dict[@"name"] = abb;
        
            }
            

        }
        [res writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf.txt"atomically:NO];
        NSData * data= [NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        
        [data writeToFile:@"/Users/wangchenglong/Desktop/vcf-oc/vcf2.txt" atomically:NO];
        //NSLog(@"%@",data);
        NSString *str = [[NSString alloc  ]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSMutableArray *res2=[NSMutableArray new];
        for(NSDictionary *dict in res)
        {
            PhoneData *p = [PhoneData new];
            [p setValuesForKeysWithDictionary:dict];
            [p setValue:@"新名字" forKey:@"name"];
            NSLog(@"new name: %@",p.name);
            Class cls = [[p valueForKey:@"num"] class];
            NSLog(@"num class: %@", NSStringFromClass(cls));
            [res2 addObject:p];
           
            
            
        }
        NSLog(@"%@",res2);
        
        
                
    }
    return 0;*/
        
 
                //8//
       // NSString*base=@"http://www.baidu.com/s";
        
        
//        NSDictionary *params =@{
//                               @"wd":@"help",
//                               
//                               };
//        NSMutableArray *arr =[NSMutableArray new];
//        [params enumerateKeysAndObjectsUsingBlock:^(NSString *key,   NSString  *obj, BOOL  *stop) {
//            NSString * p=[NSString stringWithFormat:@"%@=%@",key,obj];
//            //NSLog(@"%@=%@",key,obj);
//            [arr addObject:p];
//        }];
//        
//        NSString * pp=[arr componentsJoinedByString:@"&"];
//        pp =[pp stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//        pp=[NSString stringWithFormat:@"%@?%@",base,pp];
//        NSLog(@"%@",pp);
//        
//        NSURL *url=[NSURL URLWithString:base];
//        NSURLRequest * req=[NSURLRequest requestWithURL:url];
//        NSError * err;
//        NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];
//        if(err)
//        {
//            NSLog(@"%@",err);
//        }
//        NSString *sty=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//        
//        NSLog(@"%@",sty);
//   }
//    return 0;
//    
    
     NSString*base=@"http://www.baidu.com/s";
        
        NSURL *url=[NSURL URLWithString:base];
        NSURLRequest * req=[NSURLRequest requestWithURL:url];
        
        NSError * err;
        NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];
        if(err)
        {
           
            NSLog(@"%@",err);
        }
        NSString *sty=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",sty);
    }
    return 0;
    
 
    

}

